<?php
/*********************************************************************
    index.php

    Helpdesk landing page. Please customize it to fit your needs.

    Peter Rotich <peter@osticket.com>
    Copyright (c)  2006-2010 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
    $Id: $
**********************************************************************/
require('client.inc.php');

//We are only showing landing page to users who are not logged in.
if($thisclient && is_object($thisclient) && $thisclient->isValid()) {
    require('tickets.php');
    exit;
}
//send POST values instead of html FORM
function PostRequest($url, $_data) {
 
    // convert variables array to string:
    $data = array();    
    while(list($n,$v) = each($_data)){
        $data[] = "$n=$v";
    }    
    $data = implode('&', $data);
    // format --> test1=a&test2=b etc.
    //echo $data.'<br/>';
    // parse the given URL
    $url = parse_url($url);
    if ($url['scheme'] != 'http') { 
        die('Only HTTP request are supported !');
    }
 
    // extract host and path:
    $host = $url['host'];
    $path = $url['path'];
 
    // open a socket connection on port 80
    $fp = fsockopen($host, 8888);
 
    // send the request headers:
    fputs($fp, "POST $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Referer: $referer\r\n");
    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
    fputs($fp, "Content-length: ". strlen($data) ."\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    fputs($fp, $data);
 
    $result = ''; 
    while(!feof($fp)) {
        // receive the results of the request
        $result .= fgets($fp, 128);
    }
 
    // close the socket connection:
    fclose($fp);
 
    // split the result header from the content
    $result = explode("\r\n\r\n", $result, 2);
 
    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';
 
    // return as array:
    return array($header, $content);
}



if($_POST['create']==1) {

       if(($uID=Client::create($_POST,$errors))) {
   
       $msg=Format::htmlchars($_POST['email']).' added successfully';
       $usersdata = array();
       $usersdata['login']=$_POST['email'];
       $usersdata['pass']=$_POST['vpassword']; 
       $usersdata['create']=$_POST['create']; 
       //header('Cache-Control: public');

       session_start();
       //echo session_id().'<br />'; 
       session_destroy();
       session_write_close();  
       list($header, $content)=PostRequest('http://127.0.0.1/osticket/login.php',$usersdata);
       $regexp='PHPSESSID=([^ ]{32})';
       preg_match_all("/$regexp/siU",$header, $sessionid, PREG_SET_ORDER); 
         //echo $sessionid[0][1].'<br/>';
         //print_r($sessionid);//.'<br/>';
         //echo '<br/>'.$content.'<br/>';
       session_id($sessionid[0][1]);
       session_start();

       //echo $_SESSION['_client']['userID'].'<br/>';
       //echo session_id();
       session_write_close(); 
       session_regenerate_id();
       header('Location: tickets.php');
       require('tickets.php'); // just in case!
       exit;
       }
                elseif(!$errors['err'])
                    $errors['err']='Unable to add the user. Internal error';
}

require(CLIENTINC_DIR.'header.inc.php');
?>
<div id="index">
<h1>Welcome to the support center</h1>
<p class="big">In order to streamline support requests and better serve you, we utilize a support ticket system. Every support request is assigned a unique ticket number which you can use to track the progress and responses online. For your reference we provide complete archives and history of all your support requests. A valid email address is required.</p>
<hr />
<br />
<div class="lcol">
  <img src="./images/new_ticket_icon.jpg" width="48" height="48" align="left" style="padding-bottom:150px;">
  <h3>Register in the system</h3>If you here for the first time please create a new account.
  <br /><br />
  <form class="status_form" action="index.php" method="post">
    <input type="hidden" name="create" value="1">  
    <fieldset>
      <label>Email:</label>
      <input type="text" name="email">
    </fieldset>
    <fieldset>
     <label>Password:</label>
     <input type="password" name="npassword">
    </fieldset>
    <fieldset>
     <label>Repeat the Password:</label>
     <input type="password" name="vpassword">
    </fieldset>
    <fieldset>
        <label>&nbsp;</label>
         <input type="submit" class="button2" value="Create">
    </fieldset>
  </form>
</div>
<div class="rcol">
  <img src="./images/ticket_status_icon.jpg" width="48" height="48" align="left" style="padding-bottom:150px;">
  <h3>Login to the system</h3>Stat your work with the system from authorization.
  <br /><br />
  <form class="status_form" action="login.php" method="post">
    <fieldset>
      <label>Email:</label>
      <input type="text" name="login">
    </fieldset>
    <fieldset>
     <label>Password:</label>
     <input type="text" name="pass">
    </fieldset>
    <fieldset>
        <label>&nbsp;</label>
         <input type="submit" class="button2" value="Log me in">
    </fieldset>
  </form>
</div>
<div class="clear"></div>
<br />
</div>
<br />
<?require(CLIENTINC_DIR.'footer.inc.php'); ?>
